<?php
/**
 * Tag表的Logic
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/27
 * Time: 11:13
 */

namespace App\Http\Logics\MiniProgram;


use App\Tag;
use App\Http\Logics\BaseLogic;
class TagLogic extends BaseLogic
{
    public static function getTagList($data)
    {
        $tag = Tag::with(['tag'])->where(function($query) use ($data){
            if (isset($data['parent_id']) && $data['parent_id'] !== ''){
                $query->where('parent_id', $data['parent_id']);
            }
            if (isset($data['category_id']) && $data['category_id']){
                $query->whereHas('category', function ($query) use ($data){
                    $query->where('category.id', $data['category_id']);
                });
            }
        })->get();
        if (empty($tag)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $tag);
    }

    public static function getBrotherTagList($data)
    {
        $parent_id = Tag::select('parent_id')->find($data['tag_id'])->parent_id;
        $tag = Tag::where('parent_id', $parent_id)->get();
        if (empty($tag)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $tag);
    }
}